package com.webapp.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.Application;
import com.webapp.model.LoginModel;
import com.webapp.model.Review;
import com.webapp.model.ReviewModel;
import com.webapp.model.User;

/**
 * Servlet implementation class ReviewApp
 */
public class ReviewApp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReviewApp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		User user = (User)session.getAttribute("UserObject");
		Application app = (Application)session.getAttribute("Application");
		
		String rating = request.getParameter("ratingTxt");
		String summary = request.getParameter("summaryTxt");
		double theRating = Double.parseDouble(rating);
		
		ReviewModel rm = new ReviewModel();
		ReviewModel.enterNewReview(summary, theRating, app.getEmail(), user.getEmail());
		
		//get the request dispatcher
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/homepage.jsp");
		
		// Forward request.
		dispatcher.forward(request, response);
	}

}
